#!/bin/sh

usage ()
{
	echo "stat -c FMT FILE ..."
	exit 1
}

format=""

while getopts "c:h:?" opt ; do
	case "$opt" in
	c) format="$OPTARG" ;;
	\?|h) usage ;;
	esac
done
shift $((OPTIND - 1))

fake_device_id ()
{
	_path="$1"

	_t=$(echo "$FAKE_FILE_ID_MAP" |
	     awk -v path="${_path}" '$1 == path { print $2 }')
	_major_minor="${_t%:*}"
	_major="0x${_major_minor%:*}"
	_minor="0x${_major_minor#*:}"
	_device_id=$((_major * 256 + _minor))
	echo "$_device_id"
}

fake_inode ()
{
	_path="$1"

	_t=$(echo "$FAKE_FILE_ID_MAP" |
	     awk -v path="${_path}" '$1 == path { print $2 }')
	echo "${_t##*:}"
}

if [ -n "$format" ] ; then
	for f ; do
		if [ ! -e "$f" ] ; then
			continue
		fi
		case "$f" in
		/*) path="$f" ;;
		*) path="${PWD}/${f}" ;;
		esac

		case "$format" in
		"s#[0-9a-f]*:[0-9a-f]*:%i #%n #")
			inode=$(fake_inode "$path")
			echo "s#[0-9a-f]*:[0-9a-f]*:${inode} #${f} #"
			;;
		"%d:%i")
			device_id=$(fake_device_id "$path")
			inode=$(fake_inode "$path")
			echo "${device_id}:${inode}"
			;;
		*)
			echo "Unsupported format \"${format}\""
			usage
			;;
		esac
	done

	exit
fi

usage
